package com.property

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.baidu.location.BDLocationListener
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.search.core.SearchResult
import com.baidu.mapapi.search.geocode.*
import kotlinx.android.synthetic.main.activity_location.*

/**
 * 定位
 */
class LocationActivity : AppCompatActivity() {

    private var mBaiduMap: BaiduMap? = null
    // 定位相关声明
    var locationClient: LocationClient? = null

    private var cityName: String? = null

    private var mLocationMode: MyLocationConfiguration.LocationMode? = null
    private var mSreach: GeoCoder? = null
    private var addressStr: String? = null
    private var lLatitude: Double = 0.000
    private var lLongitude: Double = 0.000


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_location)

        initView()
    }


    private fun initView() {
        mBaiduMap = bmapView.map
        // 开启定位图层
        mBaiduMap?.isMyLocationEnabled = true
        mLocationMode = MyLocationConfiguration.LocationMode.FOLLOWING     //跟随
        mBaiduMap?.setMyLocationConfiguration(MyLocationConfiguration(mLocationMode, true, null))
        locationClient = LocationClient(applicationContext) // 实例化LocationClient类
        locationClient?.registerLocationListener(myListener) // 注册监听函数
        mBaiduMap?.setOnMapStatusChangeListener(changeListener)//地图状态改变相关监听
        setLocationOption()    //设置定位参数
        locationClient?.start() // 开始定位

        mSreach = GeoCoder.newInstance()
        mSreach?.setOnGetGeoCodeResultListener(listener)



        location.setOnClickListener {
            locationClient = LocationClient(applicationContext) // 实例化LocationClient类
            locationClient?.registerLocationListener(myListener) // 注册监听函数
            this.setLocationOption()    //设置定位参数
            locationClient?.start() // 开始定位
        }
    }

    /**
     * 设置定位参数
     */
    private fun setLocationOption() {
        val option = LocationClientOption()
        option.isOpenGps = true // 打开GPS
        option.locationMode = LocationClientOption.LocationMode.Hight_Accuracy// 设置定位模式
        option.setCoorType("bd09ll") // 返回的定位结果是百度经纬度,默认值gcj02
        option.setIsNeedAddress(true) // 返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true) // 返回的定位结果包含手机机头的方向


        locationClient?.locOption = option
    }


    /**
     * 定位监听
     */
    private var myListener: BDLocationListener = BDLocationListener { bdlocation ->
        // map view 销毁后不在处理新接收的位置
        if (location == null || bmapView == null)
            return@BDLocationListener
        val locData = MyLocationData.Builder()
                .accuracy(bdlocation.radius)
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(100f)
                .latitude(bdlocation.latitude)
                .longitude(bdlocation.longitude)
                .build()
        mBaiduMap?.setMyLocationData(locData)    //设置定位数据
        cityName = bdlocation.city
        cityName = cityName?.substring(0, cityName?.length!! - 1)

        addressStr = bdlocation.address.district + bdlocation.address.street
        location_text.text = addressStr

        lLatitude = bdlocation.latitude
        lLongitude = bdlocation.longitude
        val ll = LatLng(bdlocation.latitude, bdlocation.longitude)
        val builder = MapStatus.Builder()
        builder.target(ll).zoom(19.0f)
        mBaiduMap?.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))
    }

    /**
     * 地图状态改变相关监听
     */
    private val changeListener = object : BaiduMap.OnMapStatusChangeListener {
        override fun onMapStatusChangeStart(p0: MapStatus?, p1: Int) {

        }

        override fun onMapStatusChangeStart(p0: MapStatus?) {

        }

        override fun onMapStatusChange(p0: MapStatus?) {

        }

        override fun onMapStatusChangeFinish(mapStatus: MapStatus?) {
            //地图操作的中心点
            val cenpt = mapStatus?.target
            mSreach?.reverseGeoCode(ReverseGeoCodeOption().location(cenpt))
        }

    }


    private var listener: OnGetGeoCoderResultListener = object : OnGetGeoCoderResultListener {
        override fun onGetGeoCodeResult(result: GeoCodeResult?) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                //没有检索到结果
                return
            }
            //获取地理编码结果
        }

        override fun onGetReverseGeoCodeResult(result: ReverseGeoCodeResult?) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                //没有找到检索结果
                return
            }
            //获取反向地理编码结果
            addressStr = (result.addressDetail.street + result.addressDetail.streetNumber
                    + "\n" + result.sematicDescription)
            location_text.text = addressStr
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        // 退出时销毁定位
        locationClient?.stop()
        // 关闭定位图层
        mBaiduMap?.isMyLocationEnabled = false
        bmapView.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        bmapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        bmapView.onPause()
    }

}
